﻿' ----------------------------------------------------------------------------------
' Microsoft Developer & Platform Evangelism
' 
' Copyright (c) Microsoft Corporation. All rights reserved.
' 
' THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
' EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES 
' OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
' ----------------------------------------------------------------------------------
' The example companies, organizations, products, domain names,
' e-mail addresses, logos, people, places, and events depicted
' herein are fictitious.  No association with any real company,
' organization, product, domain name, email address, logo, person,
' places, or events is intended or should be inferred.
' ----------------------------------------------------------------------------------

Imports System.Reflection
Imports System.IO
Imports System.Data.Entity

Namespace Infrastructure
    ' Summary:
    '     Sample Entity Framework 4.1 data initializer class.
    '     The seed method is executed after the database is created using EF 4.1 Code-First, 
    '     giving the developer the opportunity to insert "seed" (i.e. initial) data.
    '     For more information, visit the ADO.NET Entity Framework website at http://msdn.microsoft.com/data/aa937723
    Public Class SqlSampleDataInitializer
        Implements IDatabaseInitializer(Of SqlDataContext)

        Public Sub InitializeDatabase(ByVal context As SqlDataContext) Implements System.Data.Entity.IDatabaseInitializer(Of SqlDataContext).InitializeDatabase
            Dim initializer = New InnerSqlSampleDataInitializer()

            ' Initialize database using EF 4.1 Code-First.
            initializer.InitializeDatabase(context)

            ' Set the command timeout to 90 seconds.
            CType(context, System.Data.Entity.Infrastructure.IObjectContextAdapter).ObjectContext.CommandTimeout = 90

            ' Create common database objects required by the ASP.NET Providers for SQL Server.
            Using stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("Microsoft.Samples.WindowsPhoneCloud.Web.InstallCommon.sql")
                Dim installCommonSqlCommands = New StreamReader(stream).ReadToEnd().Split({";"c}, StringSplitOptions.RemoveEmptyEntries)
                For Each sqlCommand In installCommonSqlCommands
                    If Not String.IsNullOrWhiteSpace(sqlCommand) Then
                        context.Database.ExecuteSqlCommand(sqlCommand)
                    End If
                Next sqlCommand
            End Using

            ' Create database objects specifically required by the ASP.NET Membership Provider for SQL Server.
            Using stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("Microsoft.Samples.WindowsPhoneCloud.Web.InstallMembership.sql")
                Dim installMembershipSqlCommands = New StreamReader(stream).ReadToEnd().Split({";"c}, StringSplitOptions.RemoveEmptyEntries)
                For Each sqlCommand In installMembershipSqlCommands
                    If Not String.IsNullOrWhiteSpace(sqlCommand) Then
                        context.Database.ExecuteSqlCommand(sqlCommand)
                    End If
                Next sqlCommand
            End Using
        End Sub

        Private Class InnerSqlSampleDataInitializer
            Inherits DropCreateDatabaseIfModelChanges(Of SqlDataContext)

            Protected Overrides Sub Seed(ByVal context As SqlDataContext)
                context.AddSampleData(1, "I am the first title", True, Date.UtcNow)
                context.AddSampleData(2, "I am the second title", True, Date.UtcNow)
            End Sub
        End Class
    End Class
End Namespace